| Conditions | 1 |
| Paths | 1 |
| Total Lines | 100 |
| Code Lines | 82 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 0 | ||
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
| 1 | const db = require("../db/database.js"); |
||
| 9 | function copyAll(res, apiKey) { |
||
| 10 | let sql = "INSERT INTO products" + |
||
| 11 | " (productId," + |
||
| 12 | " articleNumber," + |
||
| 13 | " productName," + |
||
| 14 | " productDescription," + |
||
| 15 | " productSpecifiers," + |
||
| 16 | " stock," + |
||
| 17 | " location," + |
||
| 18 | " price," + |
||
| 19 | " apiKey)" + |
||
| 20 | " SELECT productId," + |
||
| 21 | " articleNumber," + |
||
| 22 | " productName," + |
||
| 23 | " productDescription," + |
||
| 24 | " productSpecifiers," + |
||
| 25 | " stock," + |
||
| 26 | " location, " + |
||
| 27 | " price," + |
||
| 28 | "'" + apiKey + "'" + |
||
| 29 | " FROM products" + |
||
| 30 | " WHERE apiKey = ?"; |
||
| 31 | |||
| 32 | db.run(sql, copyApiKey, (err) => { |
||
| 33 | if (err) { |
||
| 34 | return res.status(500).json({ |
||
| 35 | errors: { |
||
| 36 | status: 500, |
||
| 37 | source: "/copy_products", |
||
| 38 | title: "Database error", |
||
| 39 | detail: err.message |
||
| 40 | } |
||
| 41 | }); |
||
| 42 | } else { |
||
|
|
|||
| 43 | let sql = "INSERT INTO orders" + |
||
| 44 | " (orderId," + |
||
| 45 | " customerName," + |
||
| 46 | " customerAddress," + |
||
| 47 | " customerZip," + |
||
| 48 | " customerCity," + |
||
| 49 | " customerCountry," + |
||
| 50 | " statusId," + |
||
| 51 | " apiKey)" + |
||
| 52 | " SELECT orderId," + |
||
| 53 | " customerName," + |
||
| 54 | " customerAddress," + |
||
| 55 | " customerZip," + |
||
| 56 | " customerCity," + |
||
| 57 | " customerCountry," + |
||
| 58 | " statusId," + |
||
| 59 | "'" + apiKey + "'" + |
||
| 60 | " FROM orders" + |
||
| 61 | " WHERE apiKey = ?"; |
||
| 62 | |||
| 63 | db.run(sql, copyApiKey, (err) => { |
||
| 64 | if (err) { |
||
| 65 | return res.status(500).json({ |
||
| 66 | errors: { |
||
| 67 | status: 500, |
||
| 68 | source: "/copy_orders", |
||
| 69 | title: "Database error", |
||
| 70 | detail: err.message |
||
| 71 | } |
||
| 72 | }); |
||
| 73 | } else { |
||
| 74 | let orderItemsSQL = "INSERT INTO order_items" + |
||
| 75 | " (orderId," + |
||
| 76 | " productId," + |
||
| 77 | " amount," + |
||
| 78 | " apiKey)" + |
||
| 79 | " SELECT orderId," + |
||
| 80 | " productId," + |
||
| 81 | " amount," + |
||
| 82 | "'" + apiKey + "'" + |
||
| 83 | " FROM order_items" + |
||
| 84 | " WHERE apiKey = ?"; |
||
| 85 | |||
| 86 | db.run(orderItemsSQL, copyApiKey, (err) => { |
||
| 87 | if (err) { |
||
| 88 | return res.status(500).json({ |
||
| 89 | errors: { |
||
| 90 | status: 500, |
||
| 91 | source: "/copy_orders", |
||
| 92 | title: "Database error in order_items", |
||
| 93 | detail: err.message |
||
| 94 | } |
||
| 95 | }); |
||
| 96 | } else { |
||
| 97 | return res.status(201).json({ |
||
| 98 | data: { |
||
| 99 | message: "Products and orders have been copied" |
||
| 100 | } |
||
| 101 | }); |
||
| 102 | } |
||
| 103 | }); |
||
| 104 | } |
||
| 105 | }); |
||
| 106 | } |
||
| 107 | }); |
||
| 108 | } |
||
| 109 | |||
| 219 |